/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.vehicletype;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class VehicleTypeManagerBean implements VehicleTypeManagerRemote {

    @PersistenceContext
    private EntityManager em;

    /**
     * Get all vehicle types
     * @return
     * @throws au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerException
     */
    public List<VehicleTypeTO> getAllVehicleTypes() throws VehicleTypeManagerException {
        try {
            List<VehicleTypeTO> results = new ArrayList<VehicleTypeTO>();
            Query allVehicleTypesQuery = this.em.createNamedQuery("findAllVehicleTypes");
            List dbResults = allVehicleTypesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleTypeEntity vt = (VehicleTypeEntity) dbResults.get(i);
                results.add(vt.getData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleTypeManagerException("Cannot get all vehicle types", e);
        }

    }
}
